// 文档 https://clipboardjs.com/#example-text
import Clipboard from 'clipboard'
import { Message } from 'element-ui'
export class CustomClipboard {
  private _data: string = ''
  private _class: string = ''
  private _clipboard: Clipboard
  /**
   * 描述 初始化
   * @date 2022-03-25
   * @param {any} data:string
   * @param {any} classStr:string
   * @returns {any}
   */
  constructor(data: string, classStr: string) {
    this._data = data
    this._class = classStr
    this._clipboard = new Clipboard(this._class, {
      text: () => this._data,
    })
  }

  /**
   * 描述 复制方法
   * @date 2022-03-25
   * @returns {any}
   */
  copy() {
    // 监听复制成功
    this._clipboard.on('success', () => {
      Message({
        message: '复制成功',
        type: 'success',
      })

      // 释放内存
      this._clipboard.destroy()
    })
    // 监听复制失败
    this._clipboard.on('error', () => {
      Message({
        message: '复制失败',
        type: 'error',
      })
      this._clipboard.destroy()
    })
  }
}
